<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增角色')" />
	<th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-role-add">
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">角色名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleName" id="roleName" required>
				</div>
			</div>
			<!--<div class="form-group">-->
				<!--<label class="col-sm-3 control-label is-required">权限字符：</label>-->
				<!--<div class="col-sm-8">-->
					<!--<input class="form-control" type="text" name="roleKey" id="roleKey" required>-->
					<!--<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 控制器中定义的权限字符，如：@RequiresRoles("")</span>-->
				<!--</div>-->
			<!--</div>-->
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示顺序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="roleSort" id="roleSort" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">状态：</label>
				<div class="col-sm-8">
			        <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="status" checked>
                        <span></span>
                    </label>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
<!--			<div class="row">
				<div class="col-sm-12">
					<div class="form-group">
						<label class="col-sm-3 control-label">移动端功能：</label>
						<div class="col-sm-8">
							<div style="margin-bottom: 8px;">
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="1" style="margin-right: 5px;"/><span style="margin-right: 20px;">产品出库</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="16" style="margin-right: 5px;"/><span style="margin-right: 20px;">商城客户订单</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="5" style="margin-right: 5px;"/><span style="margin-right: 20px;">待配货订单</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="2" style="margin-right: 5px;"/><span style="margin-right: 20px;">领取配货单</span>
								</div>
							</div>
							<div style="margin-bottom: 8px;">
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="7" style="margin-right: 5px;"/><span style="margin-right: 20px;">配货中订单</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="6" style="margin-right: 5px;"/><span style="margin-right: 20px;">待验货订单</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="11" style="margin-right: 5px;"/><span style="margin-right: 20px;">出库单审核</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="4" style="margin-right: 5px;"/><span style="margin-right: 20px;">待配送订单</span>
								</div>
							</div>
							<div style="margin-bottom: 8px;">
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="8" style="margin-right: 5px;"/><span style="margin-right: 20px;">已配送订单</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="12" style="margin-right: 5px;"/><span style="margin-right: 20px;">产品入库</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="17" style="margin-right: 5px;"/><span style="margin-right: 20px;">入库单审核</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="10" style="margin-right: 5px;"/><span style="margin-right: 20px;">客户退货</span>
								</div>
							</div>
							<div style="margin-bottom: 8px;">

								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="18" style="margin-right: 5px;"/><span style="margin-right: 20px;">客户退货单审核</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="14" style="margin-right: 5px;"/><span style="margin-right: 20px;">返回厂家</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="19" style="margin-right: 5px;"/><span style="margin-right: 20px;">退回厂家单审核</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="9" style="margin-right: 5px;"/><span style="margin-right: 20px;">客户信息</span>
								</div>
							</div>
							<div style="margin-bottom: 8px;">

								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="13" style="margin-right: 5px;"/><span style="margin-right: 20px;">供应商信息</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="15" style="margin-right: 5px;"/><span style="margin-right: 20px;">产品位置调拨</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="20" style="margin-right: 5px;"/><span style="margin-right: 20px;">新增产品</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="21" style="margin-right: 5px;"/><span style="margin-right: 20px;">挂起订单</span>
								</div>

							</div>

							<div style="margin-bottom: 8px;">

								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="22" style="margin-right: 5px;"/><span style="margin-right: 20px;">订单结算</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="23" style="margin-right: 5px;"/><span style="margin-right: 20px;">特价产品</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="24" style="margin-right: 5px;"/><span style="margin-right: 20px;">统计</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="25" style="margin-right: 5px;"/><span style="margin-right: 20px;">物流代收结算</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="26" style="margin-right: 5px;"/><span style="margin-right: 20px;">结算记录</span>
								</div>
							</div>

							<div style="margin-bottom: 8px;">

								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="26" style="margin-right: 5px;"/><span style="margin-right: 20px;">结算记录</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="28" style="margin-right: 5px;"/><span style="margin-right: 20px;">待入库订单</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="27" style="margin-right: 5px;"/><span style="margin-right: 20px;">日常费用</span>
								</div>
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="29" style="margin-right: 5px;"/><span style="margin-right: 20px;">调货清单</span>
								</div>
							</div>
							<div style="margin-bottom: 8px;">
								<div style="float: left">
									<input type="checkbox" name="homeMenu" value="30" style="margin-right: 5px;"/><span style="margin-right: 20px;">待入库调货单</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="31" style="margin-right: 5px;"/><span style="margin-right: 20px;">缺货提醒</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="33" style="margin-right: 5px;"/><span style="margin-right: 20px;">账务管理</span>
								</div>
								<div >
									<input type="checkbox" name="homeMenu" value="34" style="margin-right: 5px;"/><span style="margin-right: 20px;">利润管理</span>
								</div>
							</div>

						</div>
					</div>
				</div>
			</div>-->
			<div class="form-group">
				<label class="col-sm-3 control-label">菜单权限：</label>
				<div class="col-sm-8">
				    <label class="check-box">
				        <input type="checkbox" value="1">展开/折叠</label>
				    <label class="check-box">
				        <input type="checkbox" value="2">全选/全不选</label>
				    <label class="check-box">
				        <input type="checkbox" value="3" checked>父子联动</label>
				    <div id="menuTrees" class="ztree ztree-border"></div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: ztree-js" />
	<script type="text/javascript">
	    $(function() {
			var url = ctx + "system/menu/roleMenuTreeData";
			var options = {
				id: "menuTrees",
		        url: url,
		        check: { enable: true },
		        expandLevel: 0
		    };
			$.tree.init(options);
		});
		
		$("#form-role-add").validate({
			rules:{
				onkeyup: false,
				roleName:{
					remote: {
		                url: ctx + "system/role/checkRoleNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"roleName" : function() {
		                		return $.common.trim($("#roleName").val());
		                    }
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				// roleKey:{
				// 	remote: {
				//         url: ctx + "system/role/checkRoleKeyUnique",
				//         type: "post",
				//         dataType: "json",
				//         data: {
				//         	"roleKey" : function() {
				//                 return $.common.trim($("#roleKey").val());
				//             }
				//         },
				//         dataFilter: function(data, type) {
				//         	return $.validate.unique(data);
				//         }
				//     }
				// },
				roleSort:{
					digits:true
				},
			},
			messages: {
		        "roleName": {
		            remote: "角色名称已经存在"
		        },
		        /*"roleKey": {
		            remote: "角色权限已经存在"
		        }*/
		    },
		    focusCleanup: true
		});
		
		$('input').on('ifChanged', function(obj){
			var type = $(this).val();
			var checked = obj.currentTarget.checked;
			if (type == 1) {
			    if (checked) {
			        $._tree.expandAll(true);
			    } else {
			        $._tree.expandAll(false);
			    }
			} else if (type == "2") {
			    if (checked) {
			        $._tree.checkAllNodes(true);
			    } else {
			        $._tree.checkAllNodes(false);
			    }
			} else if (type == "3") {
			    if (checked) {
			        $._tree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
			    } else {
			        $._tree.setting.check.chkboxType = { "Y": "", "N": "" };
			    }
			}
		})
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	add();
	        }
	    }
	
		function add() {
			var spCodesTemp = "";
			$('input:checkbox[name=homeMenu]:checked').each(function(i){
				if(0==i){
					spCodesTemp = $(this).val();
				}else{
					spCodesTemp += (","+$(this).val());
				}
			});
			var homeMenu = spCodesTemp;
			var roleName = $("input[name='roleName']").val();
			// var roleKey = $("input[name='roleKey']").val();
			var roleSort = $("input[name='roleSort']").val();
			var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
			var remark = $("input[name='remark']").val();
			var menuIds = $.tree.getCheckedNodes();
			$.ajax({
				cache : true,
				type : "POST",
				url : ctx + "system/role/add",
				data : {
					"homeMenu": homeMenu,
					"roleName": roleName,
					// "roleKey": roleKey,
					"roleSort": roleSort,
					"status": status,
					"remark": remark,
					"menuIds": menuIds
				},
				async : false,
				error : function(request) {
					$.modal.alertError("系统错误");
				},
				success : function(data) {
					$.operate.successCallback(data);
				}
			});
		}
	</script>
</body>
</html>
